package bittorrent;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Torrent {
    /* atribut - atribut yang dimiliki oleh Torrent */
        private int piecelength;    /* ukuran (dalam byte) dari setiap piece */
        private String filename;    /* string dari nama file */
        private int filelength;     /* ukuran (dalam byte) file */
        private String announce;    /* URL dari tracker */
        private ArrayList < String > pieces = new ArrayList < String > ();
            /* string dari nilai hash sha-1 terhadap data dengan panjang 20 byte */
    
    /* constructor */
        public Torrent (String filename) throws FileNotFoundException, IOException {  //user defined ctor
            //open the file that is referred by filename
            FileInputStream fstream = new FileInputStream(filename);
            //get the object of the data input stream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader buffreadr = new BufferedReader(new InputStreamReader(in));
            String[] data = buffreadr.readLine().split(" ");
            this.filename = data[0];
            this.filelength = Integer.parseInt(data[1]);
            this.piecelength = Integer.parseInt(data[2]);
            this.announce = buffreadr.readLine();
            String datum;
            while ((datum = buffreadr.readLine()) != null) {
                this.pieces.add(datum);
            }
            //close the input stream
            in.close();
        }
    
    /* getter */
        public int getPieceLength () {
            /* mengembalikan nilai dari piecelength */
            return (piecelength);
        }
        public String getFileName () {
            /* mengembalikan nilai dari filename */
            return (filename);
        }
        public int getFileLength () {
            /* mengembalikan nilai dari filelength */
            return (filelength);
        }
        public String getAnnounce () {
            /* mengembalikan nilai dari announce */
            return (announce);
        }
        public ArrayList < String > getPieces () {
            /* mengembalikan nilai dari pieces */
            return (pieces);
        }
}
